package com.turing.post.cardmanagement.impl;

import java.math.BigInteger;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.cardmanagement.util.CardManufacture;
import com.turing.post.cardmanagement.util.CardManufactureDao;
import com.turing.post.common.util.PCPSCommonDao;
import com.turing.post.util.Config;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

/**
 * 制卡商管理Dao
 * 
 * @author 李治
 * 
 */
@Stateless(mappedName = "cardmanagement.impl.CardManufactureDaoImpl")
@Remote
@Local
public class CardManufactureDaoImpl implements CardManufactureDao {

	/**
	 * 实体管理器
	 */
	@PersistenceContext
	protected EntityManager _em;
	@EJB(mappedName = "	common.impl.PCPSCommonDaoImpl")
	private PCPSCommonDao commonDaoImpl;

	@Override
	public void deleteCardManufactureByCardManufactureId(
			BigInteger cardManufactureId) throws PCPSApplicationException {
		String sql = "delete CardManufacture t where t.cardManufactureId='"
				+ cardManufactureId + "'";
		Query query = _em.createNativeQuery(sql);
		query.executeUpdate();
	}

	@Override
	public CardManufacture getCardManufactureByCardManufactureId(
			BigInteger cardManufactureId) {
		return _em.find(CardManufacture.class, cardManufactureId);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CardManufacture> getCardManufactureHashMap(PCPSWebInput input) {
		if (input == null || PCPSUtil.isEmpty(input.getOperBranchNo()))
			return null;
		String issueCompanyNo = PCPSUtil.getIssueCompanyNo(input);
		String sql = "select t.* from CardManufacture t where issuecompanyno='"
				+ issueCompanyNo + "' order by cardManufactureId";
		Query query = _em.createNativeQuery(sql, CardManufacture.class);
		return query.getResultList();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CardManufacture> getCardManufacturesList(PCPSWebInput input) {
		if (input == null || PCPSUtil.isEmpty(input.getOperBranchNo()))
			return null;
		String icNo = PCPSUtil.getIssueCompanyNo(input);
		String sql = "select t.* from CardManufacture t where issuecompanyno='"
				+ icNo + "' order by cardManufactureId";
		Query query = _em.createNativeQuery(sql, CardManufacture.class);
		return query.getResultList();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CardManufacture> getCardManufactures(
			String cardManufactureName, String issueCompanyNo, String status,
			String linkMan, Integer page) {
		String sql = "select t.* from CardManufacture t where 1=1 ";
		if (!PCPSUtil.isEmpty(cardManufactureName))
			sql += " and cardmanufacturename like '%" + cardManufactureName
					+ "%'";
		if (!PCPSUtil.isEmpty(issueCompanyNo))
			sql += " and issuecompanyno='" + issueCompanyNo + "'";
		if (!PCPSUtil.isEmpty(status))
			sql += " and status='" + status + "'";
		if (!PCPSUtil.isEmpty(linkMan))
			sql += " and linkMan like '%" + linkMan + "%'";
		sql += "order by cardManufactureId";
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		Query query = _em.createNativeQuery(sql, CardManufacture.class)
				.setFirstResult(startPosition).setMaxResults(maxResult);
		return query.getResultList();
	}

	@Override
	public BigInteger getCardManufacturesCount(String cardManufactureName,
			String issueCompanyNo, String status, String linkMan) {
		String sql = "select nvl(count(*),0) from CardManufacture t where 1=1";
		if (!PCPSUtil.isEmpty(cardManufactureName))
			sql += " and cardmanufacturename like '%" + cardManufactureName
					+ "%'";
		if (!PCPSUtil.isEmpty(issueCompanyNo))
			sql += " and issuecompanyno='" + issueCompanyNo + "'";
		if (!PCPSUtil.isEmpty(status))
			sql += " and status='" + status + "'";
		if (!PCPSUtil.isEmpty(linkMan))
			sql += " and linkMan like '%" + linkMan + "%'";
		Query query = _em.createNativeQuery(sql);
		return new BigInteger(query.getSingleResult().toString());
	}

	@Override
	public void insertCardManufacture(CardManufacture cardManufacture)
			throws PCPSApplicationException {
		cardManufacture.setCardManufactureId(commonDaoImpl
				.getUniqueIntegerKey());
		GeneralDao.insert(_em, cardManufacture);
	}

	@Override
	public void updateCardManufacture(CardManufacture cardManufacture)
			throws PCPSApplicationException {
		GeneralDao.update(_em, cardManufacture);
	}
}
